package com.gaoyi.crm.util;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadManager {
    private static volatile ThreadManager INSTANCE;
    private final ThreadPoolExecutor mExecutor;
    private final  int mCorePoolSize;
    private final int mMaximumPoolSize;
    private final int mKeepAliveTime;

    public ThreadManager() {
        mCorePoolSize = Runtime.getRuntime().availableProcessors() * 2 +1;
        mMaximumPoolSize = mCorePoolSize;
        mKeepAliveTime  = 1;
        mExecutor = new ThreadPoolExecutor(
                mCorePoolSize,
                mMaximumPoolSize,
                mKeepAliveTime,
                TimeUnit.HOURS,
                new LinkedBlockingQueue<>()
        );
    }

    public static ThreadManager getInstance(){
        if (INSTANCE == null) {
            synchronized (ThreadManager.class){
                if (INSTANCE == null) {
                    INSTANCE = new ThreadManager();
                }
            }
        }
        return INSTANCE;
    }

    public void execute(Runnable runnable){
        if (runnable != null) {
            mExecutor.execute(runnable);
        }
    }
}
